package zarzadzaniepracownikami;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class AddWorker extends JDialog implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Worker worker;
	private Vector<Worker> listWorkers;
	
	public Worker getWorker() {
		return worker;
	}

	public void setWorker(Worker worker) {
		this.worker = worker;
	}
	
	private Vector<String> possitions;
	private DefaultListModel workers;
	private JTextField nameField;
	private JTextField surnamField;
	private JTextField PesselField;
	private JLabel lblPossition;
	private JComboBox PossitionBox;
	private JButton btnAddWorkerToDB;
	private JButton btnCancel;
	private ServerConnector connector;
	private JLabel lblNameWarnig;
	private JLabel lblPesselWarning;
	private JLabel lblSurnameWarnig;
	private JLabel lblRePassword;
	private JPasswordField rePasswordField;
	private JLabel lblPassword;
	private JPasswordField passwordField;
	private JLabel lblPasswordLabelWarning;
	private JLabel lblRepPasswordWarning;
	
	/**
	 * Create the frame.
	 */
	public AddWorker(String title, DefaultListModel workers, Vector<Worker> listWorkers ) {
		setResizable(false);
		
		connector = new ServerConnector();
		possitions=connector.GetListOfPossition();
		
		setAlwaysOnTop(true);
		setTitle(title);
		setBounds(100, 100, 371, 244);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		JLabel lblName = new JLabel("Imie:");
		lblName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblName.setVerticalAlignment(SwingConstants.TOP);
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.insets = new Insets(0, 0, 5, 5);
		gbc_lblName.gridx = 0;
		gbc_lblName.gridy = 1;
		getContentPane().add(lblName, gbc_lblName);
		
		nameField = new JTextField("");
		GridBagConstraints gbc_nameField = new GridBagConstraints();
		gbc_nameField.gridwidth = 4;
		gbc_nameField.insets = new Insets(0, 0, 5, 5);
		gbc_nameField.fill = GridBagConstraints.HORIZONTAL;
		gbc_nameField.gridx = 1;
		gbc_nameField.gridy = 1;
		getContentPane().add(nameField, gbc_nameField);
		nameField.setColumns(10);
		
		lblNameWarnig = new JLabel("   *    ");
		lblNameWarnig.setForeground(Color.RED);
		GridBagConstraints gbc_lblNameWarnig = new GridBagConstraints();
		gbc_lblNameWarnig.insets = new Insets(0, 0, 5, 0);
		gbc_lblNameWarnig.gridx = 5;
		gbc_lblNameWarnig.gridy = 1;
		lblNameWarnig.setVisible(false);
		getContentPane().add(lblNameWarnig, gbc_lblNameWarnig);
		
		JLabel lblSurname = new JLabel("Nazwisko:");
		lblSurname.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblSurname = new GridBagConstraints();
		gbc_lblSurname.insets = new Insets(0, 0, 5, 5);
		gbc_lblSurname.gridx = 0;
		gbc_lblSurname.gridy = 2;
		getContentPane().add(lblSurname, gbc_lblSurname);
		
		surnamField = new JTextField("");
		GridBagConstraints gbc_surnamField = new GridBagConstraints();
		gbc_surnamField.gridwidth = 4;
		gbc_surnamField.insets = new Insets(0, 0, 5, 5);
		gbc_surnamField.fill = GridBagConstraints.HORIZONTAL;
		gbc_surnamField.gridx = 1;
		gbc_surnamField.gridy = 2;
		getContentPane().add(surnamField, gbc_surnamField);
		surnamField.setColumns(10);
		
		lblSurnameWarnig = new JLabel("   *    ");
		lblSurnameWarnig.setForeground(Color.RED);
		GridBagConstraints gbc_lblSurnameWarnig = new GridBagConstraints();
		gbc_lblSurnameWarnig.insets = new Insets(0, 0, 5, 0);
		gbc_lblSurnameWarnig.gridx = 5;
		gbc_lblSurnameWarnig.gridy = 2;
		lblSurnameWarnig.setVisible(false);
		getContentPane().add(lblSurnameWarnig, gbc_lblSurnameWarnig);
		
		JLabel Pessel = new JLabel("Pessel:");
		GridBagConstraints gbc_Pessel = new GridBagConstraints();
		gbc_Pessel.insets = new Insets(0, 0, 5, 5);
		gbc_Pessel.gridx = 0;
		gbc_Pessel.gridy = 3;
		getContentPane().add(Pessel, gbc_Pessel);
		
		PesselField = new JTextField("");
		GridBagConstraints gbc_PesselField = new GridBagConstraints();
		gbc_PesselField.gridwidth = 4;
		gbc_PesselField.insets = new Insets(0, 0, 5, 5);
		gbc_PesselField.fill = GridBagConstraints.HORIZONTAL;
		gbc_PesselField.gridx = 1;
		gbc_PesselField.gridy = 3;
		getContentPane().add(PesselField, gbc_PesselField);
		PesselField.setColumns(10);
		
		lblPesselWarning = new JLabel("   *    ");
		lblPesselWarning.setForeground(Color.RED);
		GridBagConstraints gbc_lblPesselWarning = new GridBagConstraints();
		gbc_lblPesselWarning.insets = new Insets(0, 0, 5, 0);
		gbc_lblPesselWarning.gridx = 5;
		gbc_lblPesselWarning.gridy = 3;
		lblPesselWarning.setVisible(false);
		getContentPane().add(lblPesselWarning, gbc_lblPesselWarning);
		
		lblPassword = new JLabel("Hasło:");
		lblPassword.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblPassword.gridx = 0;
		gbc_lblPassword.gridy = 4;
		getContentPane().add(lblPassword, gbc_lblPassword);
		
		passwordField = new JPasswordField();
		GridBagConstraints gbc_passwordField = new GridBagConstraints();
		gbc_passwordField.gridwidth = 4;
		gbc_passwordField.insets = new Insets(0, 0, 5, 5);
		gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
		gbc_passwordField.gridx = 1;
		gbc_passwordField.gridy = 4;
		getContentPane().add(passwordField, gbc_passwordField);
		
		lblPasswordLabelWarning = new JLabel("   *    ");
		lblPasswordLabelWarning.setForeground(Color.RED);
		GridBagConstraints gbc_lblPasswordLabelWarning = new GridBagConstraints();
		gbc_lblPasswordLabelWarning.insets = new Insets(0, 0, 5, 0);
		gbc_lblPasswordLabelWarning.gridx = 5;
		gbc_lblPasswordLabelWarning.gridy = 4;
		lblPasswordLabelWarning.setVisible(false);
		getContentPane().add(lblPasswordLabelWarning, gbc_lblPasswordLabelWarning);
		
		lblRePassword = new JLabel("Powtórz hasło:");
		lblRePassword.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_lblRePassword = new GridBagConstraints();
		gbc_lblRePassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblRePassword.gridx = 0;
		gbc_lblRePassword.gridy = 5;
		getContentPane().add(lblRePassword, gbc_lblRePassword);
		
		rePasswordField = new JPasswordField();
		GridBagConstraints gbc_rePasswordField = new GridBagConstraints();
		gbc_rePasswordField.gridwidth = 4;
		gbc_rePasswordField.insets = new Insets(0, 0, 5, 5);
		gbc_rePasswordField.fill = GridBagConstraints.HORIZONTAL;
		gbc_rePasswordField.gridx = 1;
		gbc_rePasswordField.gridy = 5;
		getContentPane().add(rePasswordField, gbc_rePasswordField);
		
		lblRepPasswordWarning = new JLabel("   *    ");
		lblRepPasswordWarning.setForeground(Color.RED);
		GridBagConstraints gbc_lblRepPasswordWarning = new GridBagConstraints();
		gbc_lblRepPasswordWarning.insets = new Insets(0, 0, 5, 0);
		gbc_lblRepPasswordWarning.gridx = 5;
		gbc_lblRepPasswordWarning.gridy = 5;
		lblRepPasswordWarning.setVisible(false);
		getContentPane().add(lblRepPasswordWarning, gbc_lblRepPasswordWarning);
		
		lblPossition = new JLabel("Stanowisko:");
		GridBagConstraints gbc_lblPossition = new GridBagConstraints();
		gbc_lblPossition.insets = new Insets(0, 0, 5, 5);
		gbc_lblPossition.gridx = 0;
		gbc_lblPossition.gridy = 6;
		getContentPane().add(lblPossition, gbc_lblPossition);
		
		PossitionBox = new JComboBox(possitions);
		GridBagConstraints gbc_PossitionBox = new GridBagConstraints();
		gbc_PossitionBox.gridwidth = 4;
		gbc_PossitionBox.insets = new Insets(0, 0, 5, 5);
		gbc_PossitionBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_PossitionBox.gridx = 1;
		gbc_PossitionBox.gridy = 6;
		getContentPane().add(PossitionBox, gbc_PossitionBox);
		
		btnAddWorkerToDB = new JButton("Dodaj");
		GridBagConstraints gbc_btnAddWorkerToDB = new GridBagConstraints();
		gbc_btnAddWorkerToDB.insets = new Insets(0, 0, 0, 5);
		gbc_btnAddWorkerToDB.gridx = 1;
		gbc_btnAddWorkerToDB.gridy = 7;
		btnAddWorkerToDB.addActionListener(this);
		getContentPane().add(btnAddWorkerToDB, gbc_btnAddWorkerToDB);
		
		btnCancel = new JButton("Anuluj");
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.insets = new Insets(0, 0, 0, 5);
		gbc_btnCancel.gridx = 3;
		gbc_btnCancel.gridy = 7;
		btnCancel.addActionListener(this);
		getContentPane().add(btnCancel, gbc_btnCancel);
			
		this.workers=workers;
		this.listWorkers=listWorkers;
		
		
	}
	
	public boolean validateData()
	{
		String password = new String(passwordField.getPassword());
		String rePassword = new String(rePasswordField.getPassword());
		
		lblSurnameWarnig.setVisible(false);
		lblNameWarnig.setVisible(false);
		lblPesselWarning.setVisible(false);
		lblPasswordLabelWarning.setVisible(false);
		lblRepPasswordWarning.setVisible(false);
		boolean result = true;
		
		if(nameField.getText().equals("")) {
			lblNameWarnig.setVisible(true);
			result =false;
		}
		if(surnamField.getText().equals("")) { 
			lblSurnameWarnig.setVisible(true);
			result=false;
		}
		if(PesselField.getText().equals("")) {
			lblPesselWarning.setVisible(true);
			result=false;
		}
		if(passwordField.getPassword().length==0){
			lblPasswordLabelWarning.setVisible(true);
			result= false;
		}
		if(rePasswordField.getPassword().length==0){
			lblRepPasswordWarning.setVisible(true);
			result = false;
		}
		else if(!password.equals(rePassword))
		{
			result = false;
			lblPasswordLabelWarning.setVisible(true);
			lblRepPasswordWarning.setVisible(true);
		}
		
		
		return result;
	}
	

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()==btnAddWorkerToDB)
		{
			worker = new Worker();
				
			if(validateData()){
				worker.setName(nameField.getText());
				worker.setSurname(surnamField.getText());
				worker.setPesel(PesselField.getText());
				worker.setPosition(possitions.get(PossitionBox.getSelectedIndex()));
				worker.setPassword(new String(passwordField.getPassword()));
				if(!workers.contains(worker.getName()+" "+worker.getSurname()+" "+worker.getPosition())&&!listWorkers.contains(worker)){
					if(connector.AddWorker(worker)) {
						worker.setId(listWorkers.lastElement().getId()+1);
						Date date = new Date();
						worker.setFirstDayOfWork(date);
						workers.addElement(worker.getId()+" "+worker.getName()+" "+worker.getSurname()+" "+worker.getPosition());
						listWorkers.add(worker);
						JOptionPane.showMessageDialog(rootPane, "Dodano pracownika");
						this.dispose();
					}else JOptionPane.showMessageDialog(rootPane, "Niedodano pracownika do bazy");
				}
				else JOptionPane.showMessageDialog(rootPane, "Niedodano pracownika do bazy");
			}
			
		}
		else if(e.getSource()==btnCancel)
		{
			this.dispose();
		}
	}

}
